// 注册用户函数
function registerUser() {
    const username = document.getElementById('register-username').value;
    const password = document.getElementById('register-password').value;
    fetch('http://localhost:3000/register', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ username, password })
    })
   .then(response => response.json())
   .then(data => {
      if (data.success) {
        alert('注册成功，请登录');
        window.location.href = 'login.html';
      } else {
        alert('注册失败：' + data.message);
      }
    })
   .catch(error => {
      console.error(error);
      alert('网络错误，请稍后再试');
    });
  }
  
  // 登录用户函数
  function loginUser() {
    const username = document.getElementById('login-username').value;
    const password = document.getElementById('login-password').value;
    fetch('http://localhost:3000/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ username, password })
    })
   .then(response => response.json())
   .then(data => {
      if (data.success) {
        window.location.href = 'success.html';
      } else {
        alert('登录失败：' + data.message);
      }
    })
   .catch(error => {
      console.error(error);
      alert('网络错误，请稍后再试');
    });
  }